package com.gamificationlibrary.fragment;


import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;

import com.gamificationlibrary.adapter.SpinnerColorAdapter;
import com.gamificationlibrary.gamificationlibrary.R;
import com.gamificationlibrary.model.leaderboard.Leaderboard;
import com.gamificationlibrary.view.LeaderboardView;

public class SpinnerLeaderboardsFragment extends Fragment{

	private ArrayList<Leaderboard> leaderboards;
	
	private String[] fieldSpinner;
	private String playerIdentification;
	private int[] colors;

	@SuppressWarnings("unchecked")
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
	{
        // Inflate the layout for this fragment
        final View rootView= inflater.inflate(R.layout.spinnerleaderboards_fragment, container, false);
        
    	Bundle bundle=getArguments();
		leaderboards = (ArrayList<Leaderboard>) bundle.get(LeaderboardView.EXTRAS_LEADERBOARD);
		playerIdentification= bundle.getString(LeaderboardView.EXTRAS_PLAYER);
		
        Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner_leaderboard);
    
         final ArrayList<String> textarray = new ArrayList<String>();
         for(int i=0;i<leaderboards.size();i++)
     	{
     		if(this.fieldSpinner!=null)
     			textarray.add(fieldSpinner[i]);
     		else
     		{
     			textarray.add("Spinner"+i);
     		}
     	}
         
        SpinnerColorAdapter spinnerAdapter = new SpinnerColorAdapter(getActivity(), textarray, colors); 
        spinner.setAdapter(spinnerAdapter);
        spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			private LeaderboardView leaderboardView1;

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				leaderboardView1=(LeaderboardView) rootView.findViewById(R.id.leaderboardView_Leaderboard);
	   	    	leaderboardView1.putContent(leaderboards.get(position), playerIdentification);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub
				
			}
		});
    	
		return rootView; 
    }
	
	public void setFieldsSpinner(String[] fieldsNames)
	{
		this.fieldSpinner=fieldsNames;
	}
	
	public void setColorTextSpinner(int[] color)
	{
		this.colors=color;
	}
}
